package org.example.text;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther:Liu
 * @Testname:SpringHash
 * @Date:2025/8/11 17:45
 */
@SpringBootTest
@Slf4j
public class SpringHash {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void a() {
        HashOperations<String, Object, Object> forHash = stringRedisTemplate.opsForHash();
        forHash.put("A", "a", "aa");
        forHash.put("A", "b", "21");
        forHash.put("A", "c", "kkl");
        Map<String, String> map = new HashMap<>();
        map.put("d", "dd");
        map.put("e", "ee");
        forHash.putAll("A", map);
        forHash.putIfAbsent("A", "a", "aa2");
        forHash.putIfAbsent("A", "k", "kk");

    }


    @Test
    public void b() {
        HashOperations<String, String, String> forHash = stringRedisTemplate.opsForHash();
        String s = forHash.get("A", "a");
        log.info("s: {}", s);
        Map<String, String> a = forHash.randomEntries("A", 4);
        log.info("a: {}", a);
        Map.Entry<String, String> a1 = forHash.randomEntry("A");
        log.info("a1: {}", a1);
        String s1 = forHash.randomKey("A");
        log.info("s1: {}", s1);
        List<String> a2 = forHash.randomKeys("A", 4);
        log.info("a2: {}", a2);
        Map<String, String> a3 = forHash.entries("A");
        log.info("a3: {}", a3);
    }


    @Test
    public void c() {
        HashOperations<String, String, String> forHash = stringRedisTemplate.opsForHash();
        Long l = forHash.delete("A", "a");
        log.info("l: {}", l);
        Long increment = forHash.increment("A", "b", 2);
        log.info("increment: {}", increment);
        Boolean b = forHash.hasKey("A", "b");
        log.info("b: {}", b);
    }
}
